package com.quanshu.shop.base.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.quanshu.exchange.R;

/**
 * 作者  李哲
 * 创建时间 2018/7/19
 * 邮箱 lizhetongliao@163.com
 * 说明
 **/
public class BaseTitleBar extends LinearLayout implements View.OnClickListener {
    private Context mContext;
    private RelativeLayout relativeLayout;
    private RelativeLayout relativeLayoutLeft;
    private RelativeLayout relativeLayoutRight;
    private ImageView imageViewLeft;
    private ImageView imageViewRight;
    private TextView textViewCenter;
    private TextView textViewRight;
    private OnClickBaseTitleBarListener onClickBaseTitleBarListener;

    public BaseTitleBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater.from(mContext).inflate(R.layout.layout_base_title_bar, this);
        relativeLayout = findViewById(R.id.base_title_bar_rl);
        relativeLayoutLeft = findViewById(R.id.base_title_bar_left_rl);
        relativeLayoutRight = findViewById(R.id.base_title_bar_right_rl);
        relativeLayoutLeft.setOnClickListener(this);
        relativeLayoutRight.setOnClickListener(this);
        imageViewLeft = findViewById(R.id.base_title_bar_left_img);
        imageViewRight = findViewById(R.id.base_title_bar_right_img);
        textViewCenter = findViewById(R.id.base_title_bar_center_txt);
        textViewRight = findViewById(R.id.base_title_bar_right_txt);
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.BaseTitleBar);
        int bacColor = typedArray.getInteger(R.styleable.BaseTitleBar_bagd, Color.parseColor("#00ffffff"));
        typedArray.recycle();
        relativeLayout.setBackgroundColor(bacColor);
    }

    public BaseTitleBar setLeftImage(int id) {
        imageViewLeft.setImageResource(id);
        return this;
    }

    public BaseTitleBar setRightImage(int id) {
        imageViewRight.setImageResource(id);
        return this;
    }

    public BaseTitleBar setRightTextColor(int id) {
        textViewRight.setTextColor(id);
        return this;
    }

    public BaseTitleBar setRightText(String text) {
        textViewRight.setText(text);
        return this;
    }

    public BaseTitleBar setCenterText(String txt) {
        textViewCenter.setText(txt);
        return this;
    }

    public BaseTitleBar setOnClickBaseTitleListener(OnClickBaseTitleBarListener onClickBaseTitleListener1) {
        onClickBaseTitleBarListener = onClickBaseTitleListener1;
        return this;
    }

    public BaseTitleBar setBackGroudColor(int backGroudColor) {
        relativeLayout.setBackgroundColor(backGroudColor);
        return this;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.base_title_bar_left_rl:
                if (onClickBaseTitleBarListener != null) {
                    onClickBaseTitleBarListener.onclickLeft();
                }
                break;
            case R.id.base_title_bar_right_rl:
                if (onClickBaseTitleBarListener != null) {
                    onClickBaseTitleBarListener.onclickRight();
                }
                break;
        }
    }

    public interface OnClickBaseTitleBarListener {
        void onclickLeft();

        void onclickRight();
    }

}
